
import java.util.ArrayList;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;

public class QuestionSystem{
    private ArrayList<Question> questions;
    private int numberOfQuestionUsed; //holds the number of questions used
    private DataBaseManagement dataBaseManagement;//holds a DataBaseManagement object for using the methods provided
	private boolean withDataBase;

    public QuestionSystem(){
	    getConnectionToDatabase();
		readQuestion();
    }

    public int getNumberOfQuestionUsed() {
        return numberOfQuestionUsed;
    }

    public void setNumberOfQuestionUsed(int numberOfQuestionUsed) {
        this.numberOfQuestionUsed = numberOfQuestionUsed;
    }

    public ArrayList<Question> getQuestions() {
        return questions;
    }

    public void setQuestions(ArrayList<Question> questions) {
        this.questions = questions;
    }
    
     public void setwithDataBase(boolean withDataBase){
	     this.withDataBase = withDataBase;
     }
     
     public boolean isWithDataBase(){
	     return withDataBase;
     }
    
     
     public void getConnectionToDatabase(){
	    dataBaseManagement = new DataBaseManagement(); //create a DataBaseManagement for using the methods provided
		dataBaseManagement.init(); //access the database created
		if(dataBaseManagement.getConnection() != null)
			withDataBase = true;
     }

    /*
     * Function takes no prarmeter.
     * Function reads all the questions stored in database or file and assigns to its attribute questions.
     * Function returns nothing.
     */
    public void readQuestion(){
		if(withDataBase)
			questions = dataBaseManagement.readQuestinoFromDataBase(); //preforms the read operation
		else
			readQuestionFromFile("qdb.txt");
    }

    /*
     * Function takes a single parameter : 
     * numberOfQuestion - holds the number of questions needed to be read from the database or file.
     * Function reads specified number of questions stored in database or file and assigns to its attribute questions.
     * Function returns nothing.
     */
    public void readQuestion(int numberOfQuestion){
		if(withDataBase)
			questions = dataBaseManagement.readQuestinoFromDataBase(numberOfQuestion); //preforms the read operation
		else
			readQuestionFromFile("qdb.txt",numberOfQuestion);
    }


    /*
     * Function takes no prarmeter.
     * Function returns the question available.
     */
     public Question generateQuestion(){
	     //int rand = (int)(Math.random()*questions.size());
	     //return questions.get(rand);
	     return questions.get(0);
     }
     
     
	public void readQuestionFromFile(String fileName){
		
		java.util.ArrayList<Question> questionList = new java.util.ArrayList<Question>();
		try{
			
			FileReader fr = new FileReader(fileName);
			BufferedReader br = new BufferedReader(fr);//for reading file
			
			String line = ""; //holds each line read from the data file
			String[] attributes; //used to hold the field going to be store into the database
			
			//read the data file line by line
			while((line = br.readLine())!=null){
				//read all the lines in the file
				
				attributes = line.split(";"); //split the line into strings
				
				if(attributes[1].trim().equals("1")){
					ImageChoice[] imageChoices = new ImageChoice[4];
					imageChoices[0] = new ImageChoice(attributes[2].trim());
					imageChoices[0].setAnswer(true);
	
					imageChoices[1] = new ImageChoice(attributes[3].trim());
					imageChoices[2] = new ImageChoice(attributes[4].trim());
					imageChoices[3] = new ImageChoice(attributes[5].trim());
					Question question = new Question(attributes[0].trim(),imageChoices);
					question.setImageQuestion(true);
					questionList.add(question);
				}else if(attributes[1].trim().equals("0")){
					Choice[] choices = new Choice[4];
					choices[0] = new Choice(attributes[2].trim());
					choices[0].setAnswer(true);
	
					choices[1] = new Choice(attributes[3].trim());
					choices[2] = new Choice(attributes[4].trim());
					choices[3] = new Choice(attributes[5].trim());
					Question question = new Question(attributes[0].trim(),choices);
					//question.setImageQuestion(false);
					questionList.add(question);
				}
			}
			
			br.close(); //close the file
		}catch(IOException e){
			System.err.println(e.getMessage());
		}
		questions = questionList;
	}

	
	public void readQuestionFromFile(String fileName, int numberOfQuestion){
		
		java.util.ArrayList<Question> questionList = new java.util.ArrayList<Question>();
		int count = 0;
		try{
			
			FileReader fr = new FileReader(fileName);
			BufferedReader br = new BufferedReader(fr);//for reading file
			
			String line = ""; //holds each line read from the data file
			String[] attributes; //used to hold the field going to be store into the database
			
			//read the data file line by line
			while(count++<numberOfQuestion && (line = br.readLine())!=null){
				//read all the lines in the file
				
				attributes = line.split(";"); //split the line into strings
				
				if(attributes[1].trim().equals("1")){
					ImageChoice[] imageChoices = new ImageChoice[4];
					imageChoices[0] = new ImageChoice(attributes[2].trim());
					imageChoices[0].setAnswer(true);
	
					imageChoices[1] = new ImageChoice(attributes[3].trim());
					imageChoices[2] = new ImageChoice(attributes[4].trim());
					imageChoices[3] = new ImageChoice(attributes[5].trim());
					Question question = new Question(attributes[0].trim(),imageChoices);
					question.setImageQuestion(true);
					questionList.add(question);
				}else if(attributes[1].trim().equals("0")){
					Choice[] choices = new Choice[4];
					choices[0] = new Choice(attributes[2].trim());
					choices[0].setAnswer(true);
	
					choices[1] = new Choice(attributes[3].trim());
					choices[2] = new Choice(attributes[4].trim());
					choices[3] = new Choice(attributes[5].trim());
					Question question = new Question(attributes[0].trim(),choices);
					//question.setImageQuestion(false);
					questionList.add(question);
				}
			}
			
			br.close(); //close the file
		}catch(IOException e){
			System.err.println(e.getMessage());
		}
		questions = questionList;
	}

/*
	public static void main(String para[]){
		QuestionSystem app = new QuestionSystem();
		app.readQuestion();
		for(Question question: app.questions)
			System.out.println(question.getQuestionContent()+" "+question.isImageQuestion()+" "+question.getCorrectChoiceIndex());
	}
*/    
    
}
